﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using Bymed.SpineScan.Service.Shared;

namespace Bymed.SpineScan.WPF.Converter
{
    /// <summary>
    /// Converter that returns Width for Item.
    /// </summary>
    public class TestResultChartItemWidthConverter : IMultiValueConverter
    {
        /// <summary>
        /// Converter that returns right Margin for Chart Range.
        /// </summary>
        /// <param name="values">
        /// 0, ChartContainer.ActualWidth. 
        /// 1, MaxValue. 
        /// 2, CurrentValue.
        /// </param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns>Right Margin</returns>
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var chartContainerWidth = ColumnChartHeper.ConvertToDobule(values[0]);
            var maxValue = ColumnChartHeper.ConvertToDobule(values[1]);
            var currentValue = ColumnChartHeper.ConvertToDobule(values[2]);

            if (chartContainerWidth > 0 && maxValue > 0)
            {
                double ratio = currentValue / maxValue;
                var width = chartContainerWidth * ratio;

                return width;
            }
            else
            {
                return null;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Converter that returns Width for Item.
    /// </summary>
    public class TestResultChartLastItemWidthConverter : IMultiValueConverter
    {
        /// <summary>
        /// Converter that returns right Margin for Chart Range.
        /// </summary>
        /// <param name="values">
        /// 0, ChartContainer.ActualWidth. 
        /// 1, MaxValue. 
        /// 2, CurrentValue.
        /// </param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns>Right Margin</returns>
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var chartContainerWidth = ColumnChartHeper.ConvertToDobule(values[0]);
            var maxValue = ColumnChartHeper.ConvertToDobule(values[1]);
            var currentValue = ColumnChartHeper.ConvertToDobule(values[2]);

            if (chartContainerWidth > 0 && maxValue > 0)
            {
                double ratio = (maxValue - currentValue) / maxValue;
                var width = chartContainerWidth * ratio;

                return width;
            }
            else
            {
                return null;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public static class ColumnChartHeper
    {
        /// <summary>
        /// Trying to convert the provided value to double. If the conversion fails then -1 will be returned.
        /// </summary>
        /// <param name="value">The provided value needs to be covnerted to double.</param>
        /// <returns>Converted value will be returned. -1 will be return if failed.</returns>
        public static double ConvertToDobule(object value)
        {
            return value == DependencyProperty.UnsetValue ? -1 : System.Convert.ToDouble(value);
        }

        /// <summary>
        /// Calculate the column chart series range start indicator margin value
        /// </summary>
        /// <param name="rangeStartValue">The start or end value of current chart range.</param>
        /// <returns>The value for margin setup.</returns>
        public static double GetColumnChartSeriesRangeIndicatorMarginValue(double rangeValue)
        {
            if (rangeValue >= 10)
            {
                return -10;
            }
            else
            {
                return -6;
            }
        }

        /// <summary>
        /// Calculate the column chart series range margin value
        /// </summary>
        /// <param name="chartContainerWidth">The chart container width.</param>
        /// <param name="maxValue">The max value of current chart range.</param>
        /// <param name="rangeStartValue">The start value of current chart range.</param>
        /// <returns>The value for margin setup.</returns>
        public static double GetColumnChartSeriesRangeMarginValue(double chartContainerWidth, double maxValue, double rangeStartValue)
        {
            var averageWidth = chartContainerWidth / maxValue;
            var marginValue = averageWidth * rangeStartValue;

            return marginValue;
        }
    }
}
